<?php

namespace App\Http\Controllers\Asset;

use App\Http\Controllers\Controller;
use App\Http\Controllers\pagniateable;
use App\Models\AssetCategory;
use App\Models\FilterableAssetBundle\FilterableAssetBundle;
use App\Models\FilterableAssetBundle\FilterableAssetBundleCriterion;
use Illuminate\Support\Str;

class FilterableAssetBundleController extends Controller
{
    use pagniateable, FilterableBundleCriterionTrait;


    public function delete($id)
    {
        $result = [
            "success" => true,
            "message" => ""
        ];
        FilterableAssetBundle::whereId($id)->delete();
        return response()->json(compact(['result']));
    }

    public function update($id)
    {
        $bundle = FilterableAssetBundle::findOrFail($id);
        $name = request()->get("name");
        $desc = request()->get("description");
        $desc = $desc == null ? "" : $desc;
        $bundle->name = $name;
        $bundle->description = $desc;
        $bundle->save();
        $assetBundle = $bundle;
        return response()->json(compact(['assetBundle']));

    }

    public function add()
    {
        $isExists = FilterableAssetBundle::whereName(request()->get("name"))->count() > 0;
        if ($isExists) {
            $result = [
                "error" => true,
                "message" => "名称已经存在"
            ];
            return response()->json(compact(['result']));
        }
        $name = request()->get("name");
        $desc = request()->get("description");
        $desc = $desc == null ? "" : $desc;
        $asset = FilterableAssetBundle::create([
            "name" => $name,
            "description" => $desc,
            "created_by" => auth()->user()->id
        ]);
        $result = $asset;
        return response()->json(compact(['result']));

    }

    public function list()
    {
        $query = FilterableAssetBundle::select("*");
        return $this->paginate($query);

    }

    public function listCriteria($id)
    {
        $bundleItemsQuery = FilterableAssetBundleCriterion::whereBundleId($id);
        return $this->paginate($bundleItemsQuery);
    }

    public function addCriterion($id)
    {
        $bundle = FilterableAssetBundle::findOrFail($id);
        $criterion = FilterableAssetBundleCriterion::create([
            "bundle_id" => $bundle->id,
            "asset_type" => request()->get("asset_type"),
            "field_name" => request()->get("field_name"),
            "field_value" => request()->get("field_value"),
            "operator" => request()->get("operator"),
            "quantity" => request()->get("quantity")
        ]);
        $bundle->refresh()->with(["criteria"]);
        $result = $bundle;
        return response()->json(compact(['result']));
    }

    public function updateCriterion($id)
    {
        $criterion = FilterableAssetBundleCriterion::findOrFail($id);
        $criterion->update([
            "asset_type" => request()->get("asset_type"),
            "field_name" => request()->get("field_name"),
            "field_value" => request()->get("field_value"),
            "operator" => request()->get("operator"),
            "quantity" => request()->get("quantity")
        ]);
        $result = $criterion;
        return response()->json(compact(['result']));
    }

    public function delCriterion($id)
    {
        $criterion = FilterableAssetBundleCriterion::find($id);
        $criterion->delete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }

    public function delCriteriaByIds()
    {
        $ids = request()->get("ids");
        $items = FilterableAssetBundleCriterion::whereIn("id", $ids);
        $items->forceDelete();
        $result = 'ok';
        return response()->json(compact(['result']));
    }


    public function criterionFilterResult($id)
    {
        $query = $this->getCriterionQueryByFilterableAssetBundleId($id);
        return $this->paginate($query);
    }
}
